from rest_framework import serializers
from . import models


# 展示课程列表的序列化器
class CourseCategoryModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.CourseCategory
        fields = ['id', 'name']


# 直接关系表连表问题解决方式一
'''
class CourseModelSerializer(serializers.ModelSerializer):
    """展示课程详细信息+老师信息+课时信息的序列化器"""

    # 直接关系表解决连表问题
    teacher_name = serializers.CharField(max_length=32, source='teacher.name')
    teacher_title = serializers.CharField(max_length=32, source='teacher.title')

    class Meta:
        model = models.Course
        fields = [
            "id",
            "name",
            "course_img",
            "students",
            "lessons",
            "pub_lessons",
            "price",
            "teacher_name",
            "teacher_title"
        ]
'''


# 直接关系表连表问题解决方式二
class TeacherModelSerializer(serializers.ModelSerializer):

    class Meta:
        model = models.Teacher
        fields = ["id", "name", "role", "image", "brief", "title", "signature"]


# 展示课程详细信息+老师信息+课时信息的序列化器
class CourseModelSerializer(serializers.ModelSerializer):

    # 课程对应老师是多对一，老师是一
    teacher = TeacherModelSerializer()
    # 如果老师对应课程是多对一，老师是多,则在实例化序列化器对象时添加 many=True属性
    # teacher = TeacherModelSerializer(many=True)

    class Meta:
        model = models.Course
        fields = [
            "id",
            "name",
            "course_img",
            "students",
            "lessons",
            "pub_lessons",
            "price",
            'teacher',
            "lesson_list",
            "discount_name",
            "active_real_price",
        ]


# 展示课程详情页面：课程详细信息 + 老师信息 + 课时信息的序列化器
class CourseDetailModelSerializer(serializers.ModelSerializer):

    teacher = TeacherModelSerializer()

    class Meta:
        model = models.Course
        fields = [
            "id",
            "name",
            "course_img",
            "students",
            "lessons",
            "pub_lessons",
            "price",
            "teacher",
            "brief_html",
            "get_level_display",
            "get_course_type_display",
            "chapter_list",
            "course_video",
            "discount_name",
            "active_real_price",
            "active_time",
        ]
